/*
 * @Author: QMZhao
 * @Description: http请求工具函数
 * @Date: 2023-01-05 13:46:56
 * @LastEditTime: 2023-01-07 10:47:12
 * @Reference: 
 */

/**
 * 请求地址拼接
 * 
 * @param url 
 * @returns 
 */
export function setRequestUrl(url: string): string {
  const envName = import.meta.env.VITE_PROXY;
  return `${envName}/${url}`
}

/**
 * 根据状态码设置错误提示
 * 
 * @param status http 状态码
 * @returns 
 */
export function errorResponseWrapper(status: number) {
  const errorMap = new Map<number, string>([
    [400, `错误请求`],
    [401, `授权失败,请重新登录`],
    [403, `服务器拒绝访问`],
    [404, `服务器访问地址错误`],
    [405, `服务器未允许的请求方法`],
    [408, `服务器繁忙`],
    [500, `服务器错误`],
    [501, `无网络`],
    [502, `网络错误`],
    [503, `服务不可用`],
    [504, `网络信号弱`],
    [505, `不支持的请求`],
  ]);
  return errorMap.get(status) ?? `请求错误, 请检查`;
}

/**
 * 请求参数过滤空数据
 * 
 * @param params 
 * @returns 
 */
export function clearParamsWrapper<T>(params: T) {
  for (const key in params) {
    if (Object.prototype.hasOwnProperty.call(params, key)) {
      const item = params[key];
      !item && delete params[key]
    }
  }
  return params
}
